<template>
  <panel title="账号列表" :style="{ width: width + 'px' }">
    <!-- 表格 -->
    <el-table
      @selection-change="handleSelectionChange"
      ref="accountTable"
      :data="tableData"
    >
      <!-- 选择框 -->
      <el-table-column type="selection" width="55"> </el-table-column>

      <!-- 账号 -->
      <el-table-column prop="account" label="账号"> </el-table-column>

      <!-- 用户组 -->
      <el-table-column prop="userGroup" label="用户组"> </el-table-column>

     <!-- 创建时间 -->
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.ctime | filterDate }}
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)"> 编辑 </el-button>

          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 批量删除 & 取消选择 -->
    <div class="btns">
      <el-button @click="batchDelete" type="danger" size="small"> 批量删除 </el-button>
      <el-button @click="cancelSelect" type="primary" size="small"> 取消选择 </el-button>
    </div>

    <!-- 编辑弹出模态框 -->
    <el-dialog
      title="编辑账号"
      :visible.sync="dialogVisible"
      width="320px"
      :before-close="handleClose"
    >
      <!-- 表单 -->
      <el-form :model="editFormData" size="small" label-width="60px">
        <el-form-item label="账号">
          <el-input v-model="editFormData.account" class="account"></el-input>
        </el-form-item>
        <el-form-item label="用户组">
          <el-select v-model="editFormData.userGroup">
            <el-option value="普通管理员"></el-option>
            <el-option value="超级管理员"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>

        <el-button size="small" type="primary" @click="saveEdit"> 确 定 </el-button>
      </span>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </panel>
</template>

<script>
// 引入组件
import { normalizeDate } from "@/utils/tools";
import { getlist, deleteAccount, batchDel, editAccount } from "../../api/user";
import Panel from "../../components/panel/Panel.vue";

export default {
  // 注册
  components: {
    Panel,
  },
  // 数据
  data() {
    return {
      // 表格数据
      tableData: [],
      currentPage: 1, // 当前页
      total: 0, // 总数据条数
      pageSize: 5, // 每页条数
      dialogVisible: false, // 模态框显示或隐藏
      // 编辑表单数据
      editFormData: {
        account: "",
        userGroup: "",
      },
      width: document.body.clientWidth - 240,
    };
  },
  // 生命周期
  created() {
    this.getdata();
  },
  // 方法
  methods: {
    //获取数据
    async getdata() {
      try {
        let res = await getlist({
          currentPage: this.currentPage,
          pageSize: this.pageSize,
        });
        let { total, data } = res.data;
        if (!data.length && this.currentPage !== 1) {
          this.currentPage -= 1;
          this.getdata();
        }
        this.total = total;
        this.tableData = data;
      } catch (err) {
        console.log(err);
      }
    },
    //  保存修改
    async saveEdit() {
      try {
        let res = await editAccount(this.editFormData);
        if (res.data.code === 0) {
          this.dialogVisible = false;
          this.getdata();
        }
      } catch (err) {
        console.log(err);
      }
    },

    // 编辑
    handleEdit({ account, userGroup, id }) {
      this.dialogVisible = true; // 显示编辑模态框

      // 回填
      this.editFormData.account = account;
      this.editFormData.userGroup = userGroup;
      this.editFormData.id = id;
    },
    async handleDelete(row) {
      try {
        let res = await deleteAccount(row.id);
        if (res.data.code === 0) {
          this.getdata();
        }
      } catch (err) {
        console.log(err);
      }
    },
    // 每页条数变化
    handleSizeChange(val) {
      this.pageSize = val;
      this.getdata();
    },
    // 页码变化
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getdata();
    },
    // 编辑模态框关闭之前
    handleClose(done) {
      this.$confirm("确定取消编辑?")
        .then(() => {
          done(); // 关闭
        })
        .catch(() => {});
    },
    // 取消选择
    cancelSelect() {
      this.$refs.accountTable.clearSelection();
    },
    // 选项发生改变
    handleSelectionChange(rows) {
      this.ids = rows.map((v) => v.id);
    },
    // 批量删除
    batchDelete() {
      if (!this.ids || !this.ids.length) {
        this.$message.error("要选哦");
        return;
      }
      this.$confirm("批量删除不可以恢复哦", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            let res = await batchDel(JSON.stringify(this.ids));
            if (res.data.code === 0) {
              this.getdata();
            }
          } catch (err) {
            console.log(err);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消批量删除",
          });
        });
    },
  },
  filters: {
    // 过滤时间
    filterDate(time) {
      return normalizeDate(time);
    },
  },
};
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 30px;
}

.account {
  width: 194px;
}

.btns {
  margin-top: 30px;
}
</style>
